/* ********************************************************************************************
*  Alvin Baldemeca
*  Tetris Project
*  Course: TCSS 305 , Alan Fowler
*  Term: Spring 2012
*  UNIVERISTY OF WASHINGTON TACOMA
*
*
**********************************************************************************************/


package view;

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This is a class that creates a GUI that lets the user select keys and attaches
 * an action to buttons.
 * @author Alvin Baldemeca
 * @version 5/25/2012
 */
public class CustomizeKeys 
{


  /**
   * The initial height of the JFrame.
   */
  private static final int HEIGHT = 400;
  
  /**
   * The initial width of the JFrame.
   */
  private static final int WIDTH = 400;
  
  /**
   * The number of rows in the container for the grid layout.
   */
  private static final int ROWS = 6;
  
  /**
   * The frame where the options will be added to.
   */
  private JPanel my_frame;
  
 
  
  
  /**
   * Constructs a CustomizeKeys Class.
   */
  public CustomizeKeys()
  {
    setUpComponents();
  }

  /**
   * Sets up the components to display the frame and it constents.
   */
  private void setUpComponents()
  {
    my_frame = new JPanel();
    my_frame.setSize(WIDTH, HEIGHT);
    
    final JTextField text1 = new JTextField(1);
    text1.setName("text1");
    final JTextField text2 = new JTextField(1);
    text2.setName("text2");
    final JTextField text3 = new JTextField(1);
    text3.setName("text3");
    final JTextField text4 = new JTextField(1);
    text4.setName("text4");
    final JTextField text5 = new JTextField(1);
    text5.setName("text5");
    
    
//    final JLabel rotate = new JLabel(" Rotate :");
//    final JLabel left   = new JLabel(" Left   :");
//    final JLabel right  = new JLabel(" Right  :");
//    final JLabel down   = new JLabel(" Down   :");
//    final JLabel drop   = new JLabel(" Drop   :");
//    
    final JButton ok1 = new JButton();
    final JButton ok2 = new JButton();
    final JButton ok3 = new JButton();
    final JButton ok4 = new JButton();
    final JButton ok5 = new JButton();
    
    final JPanel panel1 = new JPanel();
    panel1.setName("panel1");
    //panel1.add(rotate);
    panel1.add(text1);
    panel1.add(ok1);
    
    final JPanel panel2 = new JPanel();
    panel2.setName("panel2");
    //panel2.add(left);
    panel2.add(text2);
    panel2.add(ok2);
    
    final JPanel panel3 = new JPanel();
    panel3.setName("panel3");
    //panel3.add(right);
    panel3.add(text3);
    panel3.add(ok3);
    
    final JPanel panel4 = new JPanel();    
    panel4.setName("panel4");
    //panel4.add(down);
    panel4.add(text4);
    panel4.add(ok4);
    
    final JPanel panel5 = new JPanel();
    panel4.setName("panel5");
    //panel5.add(drop);
    panel5.add(text5);
    panel5.add(ok5);
    
    final String instructions =
        "<html> Type in a key and  press button <br> Note: First value typed is used.<br>" +
        "Default contorls can still be used</html>";
    
    
    final JLabel instruction1 = new JLabel(instructions, JLabel.CENTER);
    my_frame.setLayout(new GridLayout(ROWS, 1));
    my_frame.add(instruction1);
    my_frame.add(panel1);
    my_frame.add(panel2);
    my_frame.add(panel3);
    my_frame.add(panel4);
    my_frame.add(panel5);
    

  }
  
  /**
   * Get the JPanel that has all the components needed to display to the user to choose 
   * keys to customize controls for the tetris game.
   * @return the panel that has all the buttons and text fields.
   */
  public JPanel getKeySelector()
  {
    return my_frame;
  }
  
  /**
   * Retruns the reference to the text fields on the panel to read keys from.
   * @param the_panel the panel that the user is to use the keys from.
   * @return the text field that is selected on the panel
   */
  public JTextField getTextField(final int the_panel)
  {
    final Component[] comps = my_frame.getComponents();
    final JPanel pan = (JPanel) comps[the_panel];
    return (JTextField) pan.getComponent(0);
  }
  
  
//  public String getTestField(final int the_panel)
//  {
//    //System.out.println(my_frame.getComponent(1).getName()); //TODO
//    final Component[] comps = my_frame.getComponents();
//    final JPanel pan = (JPanel) comps[the_panel];
//    return pan.getComponent(0).getName();
//
//  }
  
  /**
   * Sets the action for the buttons on the panel.
   * @param the_panel the panel that has the buttons.
   * @param the_action the action to attach to the panel.
   */
  public void setButtonAction(final int the_panel, final Action the_action)
  {
    final Component[] comps = my_frame.getComponents();
    final JPanel pan = (JPanel) comps[the_panel];
    final JButton button = (JButton) pan.getComponent(1);
    button.setAction(the_action);
  }
  
}
